package insight.google.game.era.data;

import insight.google.game.era.bo.GameRoom;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

import com.google.appengine.api.datastore.Key;

@DataTransferObject
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable="true")
@Inheritance(customStrategy = "complete-table")
public class Room extends BaseData implements Comparable<Room> {
	
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	@PrimaryKey
	protected Key roomId;
	
	@RemoteProperty
	@Persistent
	protected String name;
	
	@RemoteProperty
	@Persistent
	protected int state;
	
	@RemoteProperty
	@Persistent
	protected String owner;
	
	@RemoteProperty
	@Persistent
	protected int playerCount;
	
	@RemoteProperty
	@Persistent
	protected int dataVersion;
	
	protected Key currentGameHistoryId;
	
	
	public Room(String name) {
		this.name = name;
	}


	public Key getRoomId() {
		return roomId;
	}


	public void setRoomId(Key roomId) {
		this.roomId = roomId;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getState() {
		return state;
	}


	public void setState(int state) {
		this.state = state;
	}


	public String getOwner() {
		return owner;
	}


	public void setOwner(String owner) {
		this.owner = owner;
	}


	public int getPlayerCount() {
		return playerCount;
	}


	public void setPlayerCount(int playerCount) {
		this.playerCount = playerCount;
	}


	public int getDataVersion() {
		return dataVersion;
	}


	public void setDataVersion(int dataVersion) {
		this.dataVersion = dataVersion;
	}
	
	
	
	public Key getCurrentGameHistoryId() {
		return currentGameHistoryId;
	}


	public void setCurrentGameHistoryId(Key currentGameHistoryId) {
		this.currentGameHistoryId = currentGameHistoryId;
	}


	public void syncRoomData(GameRoom room){
		this.dataVersion +=1 ;
		this.state = room.getGameState();
		this.playerCount = room.getUserList().size();
		this.owner = room.getOwnerName();
		
	}
	
	public void reset(){
		this.currentGameHistoryId = null;
		this.owner = null;
//		this.dataVersion =0;
		this.playerCount = 0;
		this.state = 0;
		
		
	}


	@Override
	public int compareTo(Room room) {
		// TODO Auto-generated method stub
		return this.name.compareToIgnoreCase(room.getName()) ;
	}
	
	
}
